﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IntegrationTest
{
    public class TestDomain
    {
        public string Name { get; set; }

        public string Path { get; set; }

        private AppDomain mDomain;

        public Exception LastError
        {
            get;
            set;
        }
        public void Run()
        {
            Handler.Run();
        }
        public void Load()
        {
            try
            {
                if (mDomain != null)
                {
                    AppDomain.Unload(mDomain);
                }
                Type typeFromHandle = typeof(TestDomainHandler);
                AppDomainSetup appDomainSetup = new AppDomainSetup();
                appDomainSetup.ApplicationName = Name;
                appDomainSetup.ApplicationBase = Path;
                appDomainSetup.CachePath = Path + "_temp";
                appDomainSetup.ShadowCopyFiles = "true";
                appDomainSetup.ShadowCopyDirectories = Path;
                appDomainSetup.ConfigurationFile = Path + "app.config";
                mDomain = AppDomain.CreateDomain(Name, null, appDomainSetup);
               
                Handler = (TestDomainHandler)mDomain.CreateInstanceAndUnwrap(typeFromHandle.Assembly.GetName().Name, typeFromHandle.FullName);
                Handler.Load();
                
            }
            catch (Exception e_)
            {
                LastError = e_;
            }
        }

        public TestDomainHandler Handler
        {
            get;
            set;
        }
    }
}
